<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="icon" th:href="@{/img/logo.ico}" href="../static/img/logo.ico" type="images/x-ico"/>

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="../static/css/bootstrap.min.css" th:href="@{/css/bootstrap.min.css}">
    <title>学生列表</title>
    <link rel="stylesheet" th:href="@{/fontawesome/css/font-awesome.min.css}"
          href="../../static/fontawesome/css/font-awesome.min.css">

    <link rel="stylesheet"
          href="https://fonts.googleapis.com/css2?family=Poppins:ital,wght@0,500;0,600;0,700;1,400&display=swap">

    <!-- Fontawesome CSS -->
    <link rel="stylesheet" href="../static/fontawesome/css/fontawesome.min.css"
          th:href="@{/fontawesome/css/fontawesome.min.css}">
    <link rel="stylesheet" href="../static/fontawesome/css/all.min.css" th:href="@{/fontawesome/css/all.min.css}">

    <!-- Main CSS -->
    <link rel="stylesheet" href="../static/css/back_index.css" th:href="@{/css/back_index.css}">
    <style type="text/css">
        th, td {
            white-space: nowrap;
            overflow: hidden;
            word-break: keep-all;
        }
    </style>
</head>
<body>
<div class="main-wrapper">
    <div th:replace="commons/bar::#menubar(activeUri1='学生管理',activeUri2='学生列表')"></div>

    <div class="page-wrapper">
        <div class="content container-fluid">
            <div class="row">
                <div class="col-lg-12">
                    <h1>学生信息列表</h1>
                    <hr>
                    <div class="form-inline col-sm-12">
                        <label class="control-label col-sm-1" for="selectstudentid"><strong>学号</strong></label>
                        <input type="text" id="selectstudentid" class="form-control form-control-sm col-sm-3">
                        <label class="control-label col-sm-1" for="selectstudentname"><strong>姓名</strong></label>
                        <input type="text" id="selectstudentname" class="form-control form-control-sm col-sm-3">
                    </div>
                    <hr>
                    <div class="fixed-table-toolbar">
                        <div class="bs-bars pull-left">
                            <div id="toolbar" class="btn-group">
                                <button type="button" class="btn btn-primary btn-sm" data-toggle="modal"
                                        data-target="#importStudentModal">
                                    <span class="fa fa-plus" aria-hidden="true"></span> 导入学生
                                </button>
                                <button type="button" class="btn btn-info btn-sm" data-toggle="modal"
                                        data-target="#addStudentModal" id="addStudent">
                                    <span class="fa fa-plus" aria-hidden="true"></span> 添加学生
                                </button>
                                <button type="submit" class="btn btn-danger btn-sm" data-toggle="modal"
                                        id="deleteCheckBtn" disabled="disabled">
                                    <span class="fa fa-plus" aria-hidden="true"></span> 删除选中
                                </button>
                            </div>
                        </div>
                        <div class="columns columns-right btn-group pull-right">
                            <div class="keep-open btn-group" title="列">
                                <button class="btn btn-default" type="button" name="refresh" title="刷新"
                                        onclick="myrefresh()">
                                    <i class="fa fa-refresh"></i></button>
                                <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
                                    <i class="fas fa-table"></i><span class="caret"></span>
                                </button>
                                <ul class="dropdown-menu" role="menu">
                                    <li>
                                        <input type="checkbox" checked="checked" id="td1" onclick="hiddenTD('td1')">学号
                                    </li>
                                    <li>
                                        <input type="checkbox" checked="checked" id="td2" onclick="hiddenTD('td2')">姓名
                                    </li>
                                    <li>
                                        <input type="checkbox" checked="checked" id="td3" onclick="hiddenTD('td3')">班级
                                    </li>
                                    <li>
                                        <input type="checkbox" id="td4" onclick="hiddenTD('td4')">性别
                                    </li>
                                    <li>
                                        <input type="checkbox" id="td5" onclick="hiddenTD('td5')">电话
                                    </li>
                                    <li>
                                        <input type="checkbox" id="td6" onclick="hiddenTD('td6')">邮箱
                                    </li>
                                    <li>
                                        <input type="checkbox" id="td7" onclick="hiddenTD('td7')">生日
                                    </li>
                                    <li>
                                        <input type="checkbox" id="td8" onclick="hiddenTD('td8')">地址
                                    </li>
                                    <li>
                                        <input type="checkbox" checked="checked" id="td9" onclick="hiddenTD('td9')">操作
                                    </li>
                                </ul>
                            </div>
                        </div>
                    </div>
                    <div id="addStudentModal" class="modal fade" tabindex="-1" role="dialog"
                         aria-labelledby="myLargeModalLabel"
                         data-backdrop="static" data-keyboard="false">
                        <div class="modal-dialog" role="document">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <h4 class="modal-title">添加学生</h4>
                                    <button type="button" class="close" data-dismiss="modal"
                                            aria-label="Close"><span
                                            aria-hidden="true">&times;</span>
                                    </button>
                                </div>
                                <div class="modal-body">
                                    <table align="center">
                                        <tr>
                                            <td><span style="color: red">*</span>姓名</td>
                                            <td>
                                                <input type="text" id="addStudentName">
                                            </td>
                                        </tr>
                                        <tr>
                                            <td><span style="color: red">*</span>学号</td>
                                            <td>
                                                <input type="text" id="addStudentId">
                                            </td>
                                        </tr>
                                        <tr>
                                            <td><span style="color: red">*</span>班级</td>
                                            <td>
                                                <select id="addStudentClassId"></select>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>性别</td>
                                            <td>
                                                <input type="radio" value="男" name="addSex"><label
                                                    for="boy">男</label>
                                                &nbsp;&nbsp;&nbsp;&nbsp;
                                                <input type="radio" value="女" name="addSex"><label
                                                    for="girl">女</label>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>生日</td>
                                            <td>
                                                <input type="date" id="addBirthday">
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>手机号</td>
                                            <td>
                                                <input type="text" id="addTelephone"><br/>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>常用邮箱</td>
                                            <td>
                                                <input type="text" id="addEmail"><br/>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>家庭地址</td>
                                            <td>
                                                <textarea rows="2" cols="24" id="addAddress"></textarea>
                                            </td>
                                        </tr>
                                    </table>
                                </div>
                                <div class="modal-footer">
                                    <button id="submitAdd" type="button" class="btn btn-primary">添加</button>
                                    <button type="button" class="btn btn btn-secondary" data-dismiss="modal">关闭</button>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div id="importStudentModal" class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog"
                         aria-labelledby="myLargeModalLabel" data-backdrop="static" data-keyboard="false">
                        <div class="modal-dialog modal-lg" role="document">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <h4 class="modal-title">导入学生</h4>
                                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                                            aria-hidden="true">&times;</span>
                                    </button>
                                </div>
                                <div class="modal-body">
                                    <form id="importForm" class="form-horizontal">
                                        <div class="form-group">
                                            <label class="col-sm-2 control-label">格式</label>
                                            <div class="col-sm-10">
                                                <img th:src="@{/img/helpstudent.png}" width="765px">
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-sm-2 control-label">选择文件</label>
                                            <div class="col-sm-10">
                                                <input id="file" name="file" type="file" class="file"
                                                       data-show-preview="false"
                                                       data-show-upload="false"
                                                       accept="application/vnd.sealed-xls, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet">
                                            </div>
                                        </div>
                                    </form>
                                </div>
                                <div class="modal-footer">
                                    <button id="submitImport" type="button" class="btn btn-primary">导入</button>
                                    <button type="button" class="btn btn btn-secondary" data-dismiss="modal">关闭</button>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="fixed-table-container table-responsive">
                        <table class="table table-hover table-striped table-bordered" id="datas">
                            <thead>
                            <th style="max-width: 10px"><input type="checkbox" class="checkall"
                                                               onclick="mydeleteallcheck(this.checked)"></th>
                            <th class="td1">学号</th>
                            <th class="td2">姓名</th>
                            <th class="td3">班级</th>
                            <th class="td4" hidden="hidden">性别</th>
                            <th class="td5" hidden="hidden">电话</th>
                            <th class="td6" hidden="hidden">邮箱</th>
                            <th class="td7" hidden="hidden">生日</th>
                            <th class="td8" hidden="hidden">地址</th>
                            <th class="td9">操作</th>
                            </thead>
                            <tbody id="mytbody">
                            <tr id="template">
                                <td class="td0"></td>
                                <td class="td1"></td>
                                <td class="td2"></td>
                                <td class="td3"></td>
                                <td class="td4" hidden="hidden"></td>
                                <td class="td5" hidden="hidden"></td>
                                <td class="td6" hidden="hidden"></td>
                                <td class="td7" hidden="hidden"></td>
                                <td class="td8" hidden="hidden"></td>
                                <td class="td9"></td>
                            </tr>
                            </tbody>
                        </table>
                        <br/>
                        <div>
                            <div class="pull-left">
                                <input type="hidden" id="offset" value="0">
                                显示第<span id="begin"></span>到第<span id="end"></span>条记录，总共<span id="sum"></span>条记录。
                                每页显示
                                <select id="limit" onchange="this.allStudentIds = changeLimit(this.allStudentIds)">
                                    <option value="5">5</option>
                                    <option value="10" selected="selected">10</option>
                                    <option value="15">15</option>
                                    <option value="30">30</option>
                                    <option value="50">50</option>
                                    <option value="100">100</option>
                                </select> 条记录
                            </div>
                            <div class="pull-right">
                                <nav aria-label="Page navigation">
                                    <ul class="pagination" id="pages"></ul>
                                </nav>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="modal fade" id="updateStudentModal" tabindex="-1"
                     role="dialog" aria-labelledby="studentModalLabel" aria-hidden="true">
                    <div class="modal-dialog">
                        <div class="modal-content">
                            <div class="modal-header">
                                <h5 class="modal-title"
                                    id="studentModalLabel">详细信息</h5>
                                <button type="button" class="close" data-dismiss="modal"
                                        aria-label="Close">
                                    <span aria-hidden="true">&times;</span>
                                </button>
                            </div>
                            <div class="modal-body">
                                <table align="center">
                                    <tr>
                                        <td><span style="color: red">*</span>姓名</td>
                                        <td>
                                            <input type="text" id="studentName">
                                        </td>
                                    </tr>
                                    <tr>
                                        <td><span style="color: red">*</span>学号</td>
                                        <td>
                                            <input type="text" id="studentId">
                                        </td>
                                    </tr>
                                    <tr>
                                        <td><span style="color: red">*</span>班级</td>
                                        <td>
                                            <select id="studentClassId"></select>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>性别</td>
                                        <td>
                                            <input type="radio" id="boy" value="男" name="sex"><label
                                                for="boy">男</label>
                                            &nbsp;&nbsp;&nbsp;&nbsp;
                                            <input type="radio" id="girl" value="女" name="sex"><label
                                                for="girl">女</label>
                                            &nbsp;&nbsp;&nbsp;&nbsp;
                                            <input type="radio" id="null" value="其他" name="sex" disabled><label
                                                for="null">其他</label>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>生日</td>
                                        <td>
                                            <input type="date" id="birthday">
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>手机号</td>
                                        <td>
                                            <input type="text" id="telephone"><br/>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>常用邮箱</td>
                                        <td>
                                            <input type="text" id="email"><br/>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>家庭地址</td>
                                        <td>
                                            <textarea rows="2" cols="24" id="address"></textarea>
                                        </td>
                                    </tr>
                                </table>
                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-primary" id="submitResetPassword">
                                    重置密码
                                </button>
                                <button type="button" class="btn btn-info" id="submitUpdate">保存修改</button>
                                <button type="button" class="btn btn-danger" id="submitDelete">删除学生</button>
                                <button type="button" class="btn btn-secondary"
                                        data-dismiss="modal">关闭
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
                <footer>
                    <p>2020 &copy; 软件学院学生信息管理系统 by 郑焙元</p>
                </footer>
            </div>
        </div>
    </div>
</div>
<script src="../static/js/jquery-3.5.1.min.js" th:src="@{/js/jquery-3.5.1.min.js}"></script>
<script>
    var allStudentIds = [];

    var limit;
    var begin = 1;
    var end = 0;
    var pageNum;
    var currentPage = 1;

    $(function () {
        limit = $("#limit option:selected").val();
        TableInit();
        var buttonGroup = new ButtonGroup();
        buttonGroup.init();
    });

    var TableInit = function () {
        sendAjax('/admin/api/student', 'get', {
            limit: $("#limit option:selected").val(),
            offset: $("#offset").val(),
            studentid: $("#selectstudentid").val() === "" ? "-1" : $("#selectstudentid").val(),
            studentname: $("#selectstudentname").val() === "" ? "-1" : $("#selectstudentname").val(),
        }, function (res) {
            paging(res);
            $.each(res.data, function (i, student) {
                var row = $("#template").clone();
                row.find(".td0").html('<input type="checkbox" class="checks" onclick="mydeletecheck(' + student.studentid + ')">');
                row.find(".td1").text(student.studentid);
                row.find(".td2").text(student.studentname);
                row.find(".td3").text(student.studentclassid);
                row.find(".td4").text(student.sex);
                row.find(".td5").text(student.telephone);
                row.find(".td6").text(student.email);
                row.find(".td7").text(student.birthday);
                row.find(".td8").text(student.address);
                let html = '<button type="button" class="btn btn-info btn-sm" data-toggle="modal" ' +
                    'data-target="#updateStudentModal" onclick="updateStudent(\'' + student.studentid + '\')">详情</button>';
                row.find(".td9").html(html);
                row.appendTo("#datas");//添加到模板的容器中
            });
            $("#template:first").remove();
        }, '');
    }

    function myrefresh() {
        $("#mytbody").children().remove();
        $("#mytbody").html('<tr id="template">\n' +
            '                                <td class="td0"></td>\n' +
            '                                <td class="td1"></td>\n' +
            '                                <td class="td2"></td>\n' +
            '                                <td class="td3"></td>\n' +
            '                                <td class="td4" hidden="hidden"></td>\n' +
            '                                <td class="td5" hidden="hidden"></td>\n' +
            '                                <td class="td6" hidden="hidden"></td>\n' +
            '                                <td class="td7" hidden="hidden"></td>\n' +
            '                                <td class="td8" hidden="hidden"></td>\n' +
            '                                <td class="td9"></td>\n' +
            '                            </tr>');
        TableInit();
        hiddenTD("td4");
        hiddenTD("td5");
        hiddenTD("td6");
        hiddenTD("td7");
        hiddenTD("td8");
    }

    function updateStudent(studentid) {
        sendAjax('/admin/api/student/' + studentid, 'get', {}, function (res) {
            $("#studentName").val(res.data.studentname);
            $("#studentId").val(res.data.studentid);
            var id = res.data.studentclassid;
            sendAjax("/admin/api/class", "get", {}, function (res) {
                var html = "";
                $.each(res.data, function (i, data) {
                    if (data.classid === id) {
                        html += '<option value="' + data.classid + '" selected="selected">' + data.classname + '</option>';
                    } else {
                        html += '<option value="' + data.classid + '">' + data.classname + '</option>';
                    }
                })
                $("#studentClassId").html(html);
            }, "");
            $("[name = 'sex']").each(function () {
                if ($(this).val() === res.data.sex) {
                    $(this).prop("checked", "checked");
                }
            });
            $("#birthday").val(res.data.birthday === null ? "1970-01-01" : res.data.birthday.substring(0, 10));
            $("#telephone").val(res.data.telephone);
            $("#email").val(res.data.email);
            $("#address").text(res.data.address);
        }, '');
    }

    var ButtonGroup = function () {
        var object = {};


        object.init = function () {

            $('#submitImport').click(function () {
                if (confirm("确定导入学生？")) {
                    var formData = new FormData($("#importForm")[0]);
                    uploadFile("/admin/api/student/import", "post", formData, function (res) {
                        alert("导入成功");
                        $('#importStudentModal').modal('hide');
                        myrefresh();
                    }, "#submitImport");
                }
            });

            $('#submitAdd').click(function () {
                let day = new Date("1970-01-01");
                if (confirm('确认新增学生？')) {
                    sendAjax('/admin/api/student', 'post', {
                        studentname: $("#addStudentName").val(),
                        studentid: $("#addStudentId").val(),
                        studentclassid: $("#addStudentClassId option:selected").val(),
                        sex: $("[name = 'addSex']:checked").val() === undefined ? "其他" : $("[name = 'addSex']:checked").val(),
                        birthday: $("#addBirthday").val() === "" ? day : new Date($("#addBirthday").val()),
                        telephone: $("#addTelephone").val(),
                        email: $("#addEmail").val(),
                        address: $("#addAddress").val(),
                    }, function (res) {
                        alert("新增成功！");
                        $('#addStudentModal').modal('hide');
                        myrefresh();
                    }, "#submitAdd");
                }
            });

            $("#submitUpdate").click(function submitUpdate() {
                sendAjax('/admin/api/student', 'put', {
                    studentname: $("#studentName").val(),
                    studentid: $("#studentId").val(),
                    studentclassid: $("#studentClassId option:selected").val(),
                    sex: $("[name = 'sex']:checked").val(),
                    birthday: new Date($("#birthday").val()),
                    telephone: $("#telephone").val(),
                    email: $("#email").val(),
                    address: $("#address").val(),
                }, function (res) {
                    alert("修改成功！");
                    $('#updateStudentModal').modal('hide');
                    myrefresh();
                }, "#submitUpdate");
            });

            $('#submitDelete').click(function () {
                if (confirm('确认删除学生？')) {
                    sendAjax('/admin/api/student/' + $("#studentId").val(), 'delete', {}, function (res) {
                        alert("删除成功！");
                        $('#updateStudentModal').modal('hide');
                        myrefresh();
                    }, "#submitDelete");
                }
            });

            $('#deleteCheckBtn').click(function () {
                if (confirm('确认删除选中课程？')) {
                    sendAjax('/admin/api/student/list', 'delete', {
                        allStudentIds: JSON.stringify(allStudentIds),
                    }, function (res) {
                        alert("删除成功！");
                        allStudentIds = [];
                        $(".checkall").prop("checked", false);
                        $("#deleteCheckBtn").prop("disabled", "disabled");
                        myrefresh();
                    }, "#deleteCheckBtn");
                }
            });

        };

        $('#addStudent').click(function () {
            sendAjax("/admin/api/class", "get", {}, function (res) {
                var html = "";
                $.each(res.data, function (i, data) {
                    html += '<option value="' + data.classid + '">' + data.classname + '</option>';
                });
                $("#addStudentClassId").html(html);
            }, "");
        });

        $("#submitResetPassword").click(function () {
            sendAjax("/admin/api/student/" + $("#studentId").val(), 'put', {}, function (res) {
                alert("重置密码成功");
                $('#submitResetPassword').modal('hide');
                myrefresh();
            }, "#submitResetPassword");
        });

        return object;
    };

    function mydeleteallcheck(studentid) {
        this.allStudentIds = deleteallcheck(studentid, allStudentIds);
        console.log(allStudentIds)
    }

    function mydeletecheck(studentid) {
        this.allStudentIds = deletecheck(studentid, allStudentIds);
        console.log(allStudentIds)
    }

</script>
<script src="../static/js/popper.min.js" th:src="@{/js/popper.min.js}"></script>
<script src="../static/js/bootstrap.min.js" th:src="@{/js/bootstrap.min.js}"></script>

<!-- Slimscroll JS -->
<script src="../static/js/jquery.slimscroll.min.js" th:src="@{/js/jquery.slimscroll.min.js}"></script>

<!-- Custom JS -->
<script src="../static/js/script.js" th:src="@{/js/script.js}"></script>

</body>
</html>
